<?php

namespace App\Console\Commands\CustomerCoupon;

use App\Kuafu\Activity\Model\ActivityModel;
use App\Kuafu\CustomerCoupon\Model\CustomerCouponModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class RefreshActivityInfoCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'customerCoupon:refreshActivityInfo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '刷新用户优惠券活动信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        CustomerCouponModel::query()
                           ->groupBy("activityId")
                           ->select(["activityId"])
                           ->get()
                           ->each(function (CustomerCouponModel $customerCouponModel) {
                               $activity = ActivityModel::query()
                                                        ->withoutGlobalScope("isDel")
                                                        ->where("id", $customerCouponModel->activityId)
                                                        ->first();

                               if ($activity) {
                                   $this->info($activity->id . "====" . $activity->name);
                                   CustomerCouponModel::query()
                                                      ->where("activityId",
                                                          $customerCouponModel->activityId)
                                                      ->update([
                                                          "activityName" => $activity->name,
                                                          "activityType" => $activity->type
                                                      ]);
                               }

                           });

        return 0;
    }
}
